/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-10-10 16:25:31
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-10-21 08:23:21
 * @FilePath: \实训二——宏业找房\egg\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const { Controller } = require('egg');
class LiveController extends Controller {
    // 获取直播数据
    async getLive() {
        const { ctx } = this;
        const results = await this.app.mysql.select('live');
        const { value, status,  pageCode, pageSize } = ctx.request.body;
        let newLists = results;
        if (value) {
            newLists = newLists.filter(v => v.name.includes(value))
        }
        if (status) {
            newLists = newLists.filter(v => v.status.includes(status))
        }

        let total = newLists.length;
        newLists = newLists.slice((pageCode * 1 - 1) * pageSize, pageCode * pageSize)
        if (results) {
            ctx.body = {
                code: 200,
                list: newLists,
                results,
                total,
                msg: '获取成功'
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '获取失败'
            }
        }
    }
    // 删除数据
    async delLive() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('live', { id });
        if (result) {
            const results = await this.app.mysql.select('live');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list: results
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '删除失败'
            }
        }

    }

    // 状态的改变
    async changeLiveStatus() {
        const { ctx } = this;
        const { id, status } = ctx.request.body;
        // 关闭
        if (status === 'true') {
            const row = {
                status: 'false'
            };

            const options = {
                where: {
                    id
                }
            };
            const result = await this.app.mysql.update('live', row, options);
            if (result) {
                ctx.body = {
                    code: 200,
                    msg: '关闭成功',
                    status
                }
            } else {
                ctx.body = {
                    code: 500,
                    msg: '关闭失败'
                }
            }
        } 
    }
    
    // 跳详情
    // async bdDetailtData() {
    //     const { ctx } = this;
    //     // const { id } = ctx.query;
    //     const post = await this.app.mysql.get('live');
    //     // console.log(post)
    //     // if (post) {
    //     //     ctx.body = {
    //     //         code: 200,
    //     //         results: post,
    //     //         msg: "获取数据成功"
    //     //     }
    //     // } else {
    //     //     ctx.body = {
    //     //         code: 400,
    //     //         msg: "获取数据失败"
    //     //     }
    //     // }
    // }
}

module.exports = LiveController;
